class CustomerController < ApplicationController

before_filter :authorize

def add_customer
  @title = "New Customer"

    if param_posted?(:customer)
      @customer = Customer.new(params[:customer])
      if @customer.save 
        flash[:notice] = "Customer #{@customer.first_name} #{@customer.last_name} created!"
        redirect_to :controller => "employee", :action => "index"
      end
    end
  end

def edit_customer
    @customer = Customer.find(params[:id])
    @title = "Editing #{@customer.first_name} #{@customer.last_name}"
    if param_posted?(:customer)
      if @customer.update_attributes(params[:customer])
        flash[:notice] = "Customer #{@customer.first_name} #{@customer.last_name} updated"
        redirect_to :action => "list_customers" 
      end
    end
  end

  def delete_customer
  	if request.post?
		customer = Customer.find_by_id(params[:id])
		customer.destroy
		redirect_to :action => "list_customers"
  	end
  end

def list_customers
  @employee = Employee.find_by_id(session[:employee_id])

  if params[:search] == nil
      @customers = Customer.find(:all)
  else
      @customers = Customer.search params[:search]
  end
end

  def add_csv
    if request.post?
        if params[:csv]
            @csv = Csv.new(:csv_file => params[:csv][:uploaded_csv].read)
            if @csv.save!
                session[:csvid] = @csv.id

                data = []    
                @csv_num_cols = 0
                for line in @csv.csv_file do
                    x = line.split(",")
                    if x.length > @csv_num_cols
                        @csv_num_cols = x.length
                    end
                    data.push(line.split(","))
                end

                @csvdata = decode_csv(data) 
                puts @csvdata[0]
                headersPresent = false
                headerfields = [] 

                @csvdata[0].each do |col|
                    if Customer.column_names.include?(col)                        
                        headersPresent = true
#   Indexes in headerfields correspond to columns, the value contained within corresponds to Customer field names
                        headerfields.push(Customer.column_names.index(col))
                    else
                        headerfields.push(-1)
                    end
                end

                if headersPresent
                    rc = 0
                    @csvdata.each do |row|
                        e1 = Customer.new()
                        cc = 0
                        newCategoryName = ""
                        row.each do |col|
                            if (Integer(headerfields[cc]) > 0) and  rc > 0                             
                                currentFieldName = e1.class.column_names[Integer(headerfields[cc])]
                                e1[currentFieldName] = col
                            end
                            cc += 1
                        end #end row.each  (Iterating columns)                        
                        e1[:date_added] = Date.new(Time.now.year,Time.now.month,Time.now.day)
                        e1[:created_by] = session[:employee_id]
                        if rc > 0
                            e1.save
                        end
                        rc += 1
# Right now we're not doing anything if the csv specifies a category that does not exist.
                    end #end @csvdata.each (Iterating rows)
                    redirect_to :action => "list_customers"
                else
                    redirect_to :action => "map_csv"
                end #endif headersPresent
            else
                flash[:notice] = "error"
            end
        else #csv could not be saved
            flash[:notice] = "error uploading csv"
            redirect_to :controller => "employee", :action => "index"
        end #endif params[:csv]
    end #endif request.post?
  end #end def

  def map_csv
    @csv = Csv.find_by_id(session[:csvid])    
    if @csv
       @cust = Customer.new()
        @csvdata = []
        @csv_num_cols = 0;
        data = []    
        for line in @csv.csv_file do
            x = line.split(",")
            if x.length > @csv_num_cols
                @csv_num_cols = x.length
            end
            data.push(line.split(","))
        end

        @csvdata = decode_csv(data) 
    else
        flash[:notice] = "No CSV file found, try uploading it again."
        redirect_to :controller => "employee", :action => "index"
    end    

    if request.post? 
        fieldorder = params[:fieldorder].split(",")        
        rc = 0

        @csvdata.each do |row|
            e1 = Customer.new()
            cc = 0
            newCategoryName = ""
            row.each do |col|
                if (Integer(fieldorder[cc]) > 0)
                    currentFieldName = e1.class.column_names[Integer(fieldorder[cc])]           
                    e1[currentFieldName] = col
                end
                cc += 1
            end
            rc += 1
            e1[:date_added] = Date.new(Time.now.year,Time.now.month,Time.now.day)
            e1[:created_by] = session[:employee_id]
            e1.save
        end
        @csv.destroy

        flash[:notice] = "Customers uploaded"
        redirect_to :controller => "customer", :action => "list_customers"
    end
  end

private
  def param_posted?(symbol)
    request.post? and params[symbol]
  end
end

